package com.qianya.oa.controller;

import com.qianya.oa.common.annotation.Log;
import com.qianya.oa.common.core.controller.BaseController;
import com.qianya.oa.common.core.domain.AjaxResult;
import com.qianya.oa.common.core.domain.entity.SysDept;
import com.qianya.oa.common.core.domain.entity.SysUser;
import com.qianya.oa.common.core.page.TableDataInfo;
import com.qianya.oa.common.enums.BusinessType;
import com.qianya.oa.common.utils.poi.ExcelUtil;
import com.qianya.oa.domain.Project;
import com.qianya.oa.service.IProjectService;
import com.qianya.oa.service.ISysDeptService;
import com.qianya.oa.service.SysUserService;
import com.qianya.oa.util.CommonResult;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 【 工作计划】Controller
 * 
 * @author qianya
 * @date 2023-06-02
 */
@Controller
@RequestMapping("/system/project")
public class ProjectController extends BaseController
{
    private String prefix = "activiti/project";

    @Autowired
    private IProjectService projectService;

    @Autowired
    private SysUserService userService;
    @Autowired
    private ISysDeptService deptService;
    @Resource
    private TaskService taskService;
    @Resource
    private RuntimeService runtimeService;

    @RequiresPermissions("system:project:view")
    @GetMapping()
    public String project()
    {
        return prefix + "/project";
    }

    /**
     * 查询【 工作计划】列表
     */
    @RequiresPermissions("system:project:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Project project)
    {
        startPage();
        List<Project> list = projectService.selectProjectList(project);
        return getDataTable(list);
    }

    /**
     * 导出【 工作计划】列表
     */
    @RequiresPermissions("system:project:export")
    @Log(title = "【 工作计划】", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Project project)
    {
        List<Project> list = projectService.selectProjectList(project);
        ExcelUtil<Project> util = new ExcelUtil<Project>(Project.class);
        return util.exportExcel(list, "【 工作计划】数据");
    }

    /**
     * 新增【 工作计划】
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存【 工作计划】
     */
    @RequiresPermissions("system:project:add")
    @Log(title = "【 工作计划】", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Project project)
    {
        return toAjax(projectService.insertProject(project));
    }

    /**
     * 修改【 工作计划】
     */
    @RequiresPermissions("system:project:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        Project project = projectService.selectProjectById(id);
        mmap.put("project", project);
        return prefix + "/edit";
    }

    /**
     * 修改保存【 工作计划】
     */
    @RequiresPermissions("system:project:edit")
    @Log(title = "【 工作计划】", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Project project)
    {
        return toAjax(projectService.updateProject(project));
    }

    /**
     * 删除【 工作计划】
     */
    @RequiresPermissions("system:project:remove")
    @Log(title = "【 工作计划】", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(projectService.deleteProjectByIds(ids));
    }
    @Log(title = "查询用户所在部门的所有人",businessType = BusinessType.OTHER)
    @PostMapping("/getMyDeptMans")
    @ResponseBody
    public CommonResult<?> getMyDeptMans(){
        SysUser user = getSysUser();
        SysDept sysDept = deptService.selectDeptById(user.getDeptId());
        List<SysUser> myDeptAllMans = userService.selectLeaderListSecond(Math.toIntExact(user.getDeptId()),null);
        List<SysUser> myDeptAllLeaders = userService.selectLeaderListFirst(Math.toIntExact(sysDept.getDeptId()), "部门经理");
        List<List<SysUser>> deps  =new ArrayList<>();
        deps.add(myDeptAllMans);
        deps.add(myDeptAllLeaders);
        return CommonResult.success(deps);
    }
    @Log(title = "获取当前用户信息",businessType = BusinessType.OTHER)
    @PostMapping("/getMyInfo")
    @ResponseBody
    public CommonResult<?> getMyInfo(){
        SysUser user = getSysUser();
        return CommonResult.success(user);
    }
}
